import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public class AddCity {
    public static void main(String[] args) throws SQLException {
        //1.获得数据源 DataSource (设置MySql的服务器地址)
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setURL("jdbc:mysql://127.0.0.1:3306/java33?characterEncoding=utf8&useSSL=true");//1.1设置MySql的服务器地址
        dataSource.setUser("root");
        dataSource.setPassword("12345678");
        //2.得到链接
        Connection connection = (Connection) dataSource.getConnection();
        //3.得到执行器(组装sql)
        String insertSQL = "insert into city(id,name) values(?,?)";
        PreparedStatement statement = connection.prepareStatement(insertSQL);

        //填充占位符
        statement.setInt(1, 4);
        statement.setString(2, "深圳");
        //4.执行sql
        int result = statement.executeUpdate();//返回一个受影响的行数
        System.out.println("受影响的行数：" + result);
        //5.关闭资源(从小到大)
        statement.close();//关闭执行器
        connection.close();//关闭连接
    }
}
